package org.jeecg.modules.monitoring.j_monitoring.job.history;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.monitoring.j_monitoring.entity.RingWarning;
import org.jeecg.modules.monitoring.j_monitoring.service.IRingWarningService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 同步系统日志
 * @Author: xiaoyq
 */

@Slf4j
public class SyncRingWarningJob implements Job {

    @Autowired
    private IRingWarningService logService;

    private void saveToHistory(List<RingWarning> logList){
        DynamicDataSourceContextHolder.push("history");
        try{
//            for (int i = 0; i < logList.size(); i++) {
//                logService.saveOrUpdate(logList.get(i));
//            }
            logService.saveOrUpdateBatch(logList);

        }
        finally {
            DynamicDataSourceContextHolder.clear();
        }
    }

    private RingWarning getLastFromHistory(LocalDateTime date1){
        RingWarning lastEntity = null;
        DynamicDataSourceContextHolder.push("history");
        try{
            QueryWrapper<RingWarning> logHistoryQueryWrapper = new QueryWrapper();
            Long l = date1.toInstant(ZoneOffset.ofHours(8)).toEpochMilli()/1000;
            logHistoryQueryWrapper.lambda().lt(RingWarning::getEventTime,l);
            logHistoryQueryWrapper.select("max(event_time) event_time");
            lastEntity = logService.getOne(logHistoryQueryWrapper);
            return lastEntity;
        }
        finally {
            DynamicDataSourceContextHolder.clear();
        }
    }

    /***
     * 执行
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        log.info(String.format("同步动环日志数据,时间:" + DateUtils.getTimestamp()));
        LocalDateTime date1= LocalDate.parse(DateUtils.getBeginMonth(),DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay();
        date1 = date1.plusMonths(-1);//保留6个月的数据
        Long l = date1.toInstant(ZoneOffset.ofHours(8)).toEpochMilli()/1000;
        RingWarning lastEntity = this.getLastFromHistory(date1);
        QueryWrapper<RingWarning> logQueryWrapper = new QueryWrapper();

        //小于本月的第一天
        logQueryWrapper.lambda().lt(RingWarning::getEventTime,l);
        if(lastEntity!=null && lastEntity.getEventTime()!=null){
            logQueryWrapper.lambda().gt(RingWarning::getEventTime,lastEntity.getEventTime());
        }

        List<RingWarning> logList = logService.list(logQueryWrapper);
        if(logList.size()>0){
            saveToHistory(logList);
            logService.remove(logQueryWrapper);
        }
    }

}
